﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_ProDivingAdmin2salmon14
{
    public static class FileManager
    {

        public static Dictionary<string, Tuple<float, string>> diveList { get; set; }

        public static List<Diver> LoadDivers(string filePath)
        {
            List<Diver> returnDivers = new List<Diver>();

            string[] divers = System.IO.File.ReadAllLines(filePath);
            foreach (var diver in divers) 
            {
                string[] tempDiver = diver.Split(':');

                string[] tempInfo = tempDiver[0].Split(',');
                string[] tempDives = tempDiver[1].Split(',');

                List<Dive> dives = new List<Dive>();
                foreach (var tempDive in tempDives)
                {
                    bool added = false;
                    string tempStr = tempDive.Trim();
                    while (!added)
                    {  
                        if (Contest.validDives.Keys.Contains(tempStr))
                        {
                            dives.Add(new Dive(tempStr));
                            added = true;
                        }
                        else
                        {
                            added = true;
                            //Popup där användaren får veta att hoppet är ogiltigt
                            //Möjlighet ges att skriva ett hopp som är giltigt.
                            //tempStr sätts till den nya inmatningen.
                        }   
                    }
                }
                returnDivers.Add(new Diver(tempInfo[0], tempInfo[1], Int32.Parse(tempInfo[2]), tempInfo[3], dives));
            }
            return returnDivers;
        }

        public static void LoadDives(string filePath)
        {
            diveList = new Dictionary<string, Tuple<float, string>>();
            string[] dives = System.IO.File.ReadAllLines(filePath);
            foreach (var dive in dives) 
            {
                if (dive != "\n" && dive != "" && dive != " ")
                {
                    string[] temp = dive.Split(':');
                    diveList[temp[0]] = new Tuple<float, string>(float.Parse(temp[1], System.Globalization.CultureInfo.InvariantCulture), temp[2]);
                }
            } 
        }

        public static List<Judge> LoadJudges(string filePath)
        {
            Random rand = new Random();
            List<Judge> returnJudges = new List<Judge>();
            string[] judges = System.IO.File.ReadAllLines(filePath);
            foreach (var judge in judges) 
            {
                string[] temp = judge.Split(',');
                returnJudges.Add(new Judge(temp[0], temp[1], rand.Next(0, 1000)));
            }
            return returnJudges;
        }
    }
}
